package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC932 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 不是因为复杂，而是因为经典
     * 未来需要再次复习此道题目
     * 挺有意思的分治策略，我承认当初没想出来
     * @return:
     */
    class Solution {
        public Map<Integer, int[]> memo = new HashMap<>();

        public int[] beautifulArray(int n) {
            if (n == 1) return new int[]{1};
            if (n == 2) return new int[]{1, 2};
            if (memo.containsKey(n)) return memo.get(n);
            int[] curRes = new int[n];
            int[] evenPart = beautifulArray(n / 2);         // 偶部
            int[] oddPart = beautifulArray((n + 1) / 2);    // 奇部
            int i = 0;
            for (int num : oddPart) curRes[i++] = 2 * num - 1;
            for (int num : evenPart) curRes[i++] = 2 * num;
            memo.put(n, curRes);
            return curRes;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.beautifulArray(9)));
    }

    public static void main(String[] args) throws Exception {
        LC932 an = new LC932();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
